package com.jiegui.modules.common.resoler;

import com.jiegui.common.exception.user.RRException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: wuqicheng
 * @time: 28/12/2021 3:16 PM
 */
@RestControllerAdvice
@Slf4j
public class MyControllerAdvice {

//    /**
//     * 全局异常捕捉处理
//     * @param ex
//     * @return
//     */
//    @ResponseBody
//    @ExceptionHandler(value = Exception.class)
//    public Map errorHandler(Exception ex) {
//        Map<String,Object> map = new HashMap<>();
//        map.put("code", 100);
//        map.put("msg", ex.getMessage());
//        return map;
//    }

//    /** 空指针异常 */
//    @ExceptionHandler(RRException.class)
//    public Map<String,Object> rrexceptionHandler(RRException ex) {
//        log.error("异常信息"+ex);
//        Map<String,Object> map = new HashMap<>();
//        map.put("code", ex.getCode());
//        map.put("msg",ex.getMsg());
//        return map;
//    }

    /** 空指针异常 */
    @ExceptionHandler(NullPointerException.class)
    public Map<String,Object> nullPointerExceptionHandler(NullPointerException ex) {
        ex.printStackTrace();
         log.error("异常信息"+ex);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg","空指针异常");
        return map;
    }

    /** 类型转换异常 */
    @ExceptionHandler(ClassCastException.class)
    public Map<String,Object> classCastExceptionHandler(ClassCastException ex) {
        log.error("异常信息"+ex);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg", "类型转换异常");
        return map;
    }
    /** 文件未找到异常 */
    @ExceptionHandler(FileNotFoundException.class)
    public Map<String,Object> FileNotFoundException(FileNotFoundException ex) {
        log.error("异常信息"+ex);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg", "文件未找到异常");
        return map;
    }
    /** 数字格式异常 */
    @ExceptionHandler(NumberFormatException.class)
    public Map<String,Object> NumberFormatException(NumberFormatException ex) {
        log.error("异常信息"+ex);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg","数字格式异常" );
        return map;
    }
    /** 安全异常 */
    @ExceptionHandler(SecurityException.class)
    public Map<String,Object> SecurityException(SecurityException ex) {
        log.error("异常信息"+ex);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg", "安全异常");
        return map;
    }
    /** sql异常 */
    @ExceptionHandler(SQLException.class)
    public Map<String,Object> SQLException(SQLException ex) {
        log.error("异常信息"+ex);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg", "sql异常");
        return map;
    }
    /** 类型不存在异常 */
    @ExceptionHandler(TypeNotPresentException.class)
    public Map<String,Object> TypeNotPresentException(TypeNotPresentException ex) {
        log.error("异常信息"+ex);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg","类型不存在异常");
        return map;
    }

    /** IO异常 */
    @ExceptionHandler(IOException.class)
    public Map<String,Object> iOExceptionHandler(IOException ex) {
        log.error("异常信息"+ex);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg","IO异常");
        return map;
    }

    /** 未知方法异常 */
    @ExceptionHandler(NoSuchMethodException.class)
    public Map<String,Object> noSuchMethodExceptionHandler(NoSuchMethodException ex) {
        log.error("异常信息"+ex);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg", "未知方法异常");
        return map;
    }

    /** 数组越界异常 */
    @ExceptionHandler(IndexOutOfBoundsException.class)
    public Map<String,Object> indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException ex) {
        log.error("异常信息"+ex);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg","数组越界异常");
        return map;
    }
    /** sql语法错误异常 */
    @ExceptionHandler(BadSqlGrammarException.class)
    public Map<String,Object> BadSqlGrammarException(BadSqlGrammarException ex) {
        log.error("异常信息"+ex);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg","sql语法错误异常");
        return map;
    }

    /** 无法注入bean异常 */
    @ExceptionHandler(NoSuchBeanDefinitionException.class)
    public Map<String,Object> NoSuchBeanDefinitionException(NoSuchBeanDefinitionException ex) {
        log.error("异常信息"+ex);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg", "无法注入bean异常");
        return map;
    }

    /** Http消息不可读异常 */
    @ExceptionHandler({HttpMessageNotReadableException.class})
    public Map<String,Object> requestNotReadable(HttpMessageNotReadableException ex) {
        log.error("异常信息"+ex);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg", "Http消息不可读异常");
        return map;
    }

    /** 400错误 */
    @ExceptionHandler({TypeMismatchException.class})
    public Map<String,Object> requestTypeMismatch(TypeMismatchException ex) {
        log.error("异常信息"+ex);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg", ex.getMessage());
        return map;
    }

    /** 500错误 */
    @ExceptionHandler({ConversionNotSupportedException.class, HttpMessageNotWritableException.class})
    public Map<String,Object> server500(RuntimeException ex) {
        log.error("异常信息"+ex);
        Map<String,Object> map =  new HashMap<>();
        map.put("code", 100);
        map.put("msg", ex.getMessage());
        return map;
    }

    /** 栈溢出 */
    @ExceptionHandler({StackOverflowError.class})
    public Map<String,Object> requestStackOverflow(StackOverflowError ex) {
        log.error("异常信息"+ex);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg", "栈溢出"+ex.getMessage());
        return map;
    }

    /** 除数不能为0 */
    @ExceptionHandler({ArithmeticException.class})
    public Map<String,Object> arithmeticException(ArithmeticException ex) {
        log.error("异常信息"+ex);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg","除数不能为0 ");
        return map;
    }

    /** 其他错误 */
    @ExceptionHandler({Exception.class})
    public Map<String,Object> exception(Exception ex) {
        ex.printStackTrace();
        log.error("异常信息"+ex);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 100);
        if(ex instanceof  RRException){
           map.put ("code", ((RRException) ex).getCode());
        }
        map.put("msg", ex.getMessage());
        return map;
    }
}
